<style lang="less">
    @import "../../../styles/table.less";
</style>
<template>
    <div class="search">
        <Row>
            <Col>
                <Card>
                    <Row @keydown.enter.native="handleSearch">
                        <Form ref="searchForm" :model="searchForm" inline :label-width="70">
                            <Form-item label="订单号" prop="id">
                                <Input type="text" v-model="searchForm.id" placeholder="请输入订单号" clearable style="width: 200px"/>
                            </Form-item>
                            <Form-item label="下单人" prop="userName">
                                <Input type="text" v-model="searchForm.userName" placeholder="请输下单人" clearable style="width: 200px"/>
                            </Form-item>
                            <Form-item label="手机号" prop="mobile">
                                <Input type="text" v-model="searchForm.mobile" placeholder="请输下单人" clearable style="width: 200px"/>
                            </Form-item>
                            <span v-if="drop">
                                <Form-item label="支付方式" prop="payType">
                                    <Select v-model="searchForm.payType" placeholder="请选择支付方式" clearable style="width: 200px">
                                        <Option value="Alipay">支付宝</Option>
                                        <Option value="WxPay">微信</Option>
                                        <Option value="Balance">学习币</Option>
                                    </Select>
                                </Form-item>
                                <Form-item label="订单类型" prop="orderEntity">
                                <Select v-model="searchForm.orderEntity" placeholder="请选择" clearable style="width: 200px">
                                    <Option value="vip">购买VIP</Option>
                                    <Option value="top_up">充值</Option>
                                    <Option value="course">线上课程</Option>
                                    <Option value="live">直播订单</Option>
                                    <Option value="appraisal">能力测评</Option>
                                    <Option value="offcourse">线下课程</Option>
                                </Select>
                               </Form-item>
                                <Form-item label="支付时间">
                                  <DatePicker v-model="selectDate" type="daterange" format="yyyy-MM-dd" clearable @on-change="selectDateRange" placeholder="选择起始时间" style="width: 200px"></DatePicker>
                                </Form-item>
                                <Form-item label="是否开发票" prop="isInvoice">
                                    <Select v-model="searchForm.isInvoice" placeholder="请选择是否开发票" clearable style="width: 200px">
                                        <Option value="1">已开</Option>
                                        <Option value="2">待开</Option>
                                    </Select>
                                </Form-item>
                            </span>
                            <Form-item style="margin-left:-35px;" class="br">
                                <Button @click="handleSearch" type="primary" icon="ios-search">搜索</Button>
                                <Button @click="handleReset">重置</Button>
                                <a class="drop-down" @click="dropDown">{{dropDownContent}}
                                    <Icon :type="dropDownIcon"></Icon>
                                </a>
                            </Form-item>
                        </Form>
                    </Row>
                    <Row class="operation">
                        <Button @click="exportData" icon="md-trash">导出</Button>
                        <Button @click="getDataList" icon="md-refresh">刷新</Button>
                        <circleLoading v-if="operationLoading"/>
                    </Row>
                    <Row>
                        <Alert show-icon>
                            已选择 <span class="select-count">{{selectCount}}</span> 项
                            <a class="select-clear" @click="clearSelectAll">清空</a>
                        </Alert>
                    </Row>
                    <Row>
                        <Table :loading="loading" border :columns="columns" :data="data" sortable="custom" @on-sort-change="changeSort" @on-selection-change="showSelect" ref="table"></Table>
                    </Row>
                    <Row type="flex" justify="end" class="page">
                        <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize" @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]" size="small" show-total show-elevator show-sizer></Page>
                    </Row>
                </Card>
            </Col>
        </Row>
        <return-drawer ref="returnDrawer"></return-drawer>
    </div>
</template>

<script>
    import circleLoading from "../../my-components/circle-loading.vue";
    import {orderList, delOrder} from "@/api/bus";
    import invoiceDrawer from "./invoiceDrawer";
    import Util from '@/libs/util';
    import returnDrawer from "./return-drawer";

    export default {
        name: "order-manage",
        components: {
            returnDrawer,
            circleLoading
        },
        data() {
            return {
                loading: false, // 表单加载状态
                operationLoading: false,
                drop: false,
                selectCount: 0, // 多选计数
                selectList: [], // 多选数据
                dropDownContent: "展开",
                dropDownIcon: "ios-arrow-down",
                searchForm: {
                    pageNumber: 1, // 当前页数
                    pageSize: 10, // 页面大小
                    sort: "createTime", // 默认排序字段
                    order: "desc", // 默认排序方式
                    id: "",
                    orderEntity: "",
                    userName: "",
                    mobile: "",
                    startDate: "",
                    endDate: "",
                    isInvoice: ""
                },
                selectDate: null, //选择日期绑定modal
                columns: [
                    {
                        type: "selection",
                        width: 60,
                        align: "center"
                    },
                    {
                        type: "index",
                        width: 60,
                        align: "center"
                    },
                    {
                        title: "下单人",
                        key: "userName",
                        width: 115,
                        sortable: true
                    },
                    {
                        title: "联系方式",
                        key: "mobile",
                        width: 180,
                        sortable: true
                    },
                    {
                        title: "订单号",
                        key: "id",
                        width: 200,
                        sortable: true
                    },
                    {
                        title: "商品名称",
                        key: "orderTitle",
                        width: 180,
                        sortable: true
                    },
                    {
                        title: "消费类型",
                        key: "orderEntityText",
                        width: 180,
                        sortable: true
                    },
                    {
                        title: "支付方式",
                        key: "payTypeText",
                        width: 180,
                        sortable: true
                    }, {
                        title: "发票",
                        key: "isInvoice",
                        width: 180,
                        sortable: true,
                        render: (h, params) => {
                            let re = "";
                            if (params.row.isInvoice === 1) {
                                return h("div", [
                                    h(
                                        invoiceDrawer,
                                        {
                                            props: {
                                                id: params.row.id
                                            },
                                            style: {
                                                marginRight: "5px"
                                            }
                                        }
                                    )
                                ]);
                            } else if (params.row.isInvoice === 2) {
                                re = "未开";
                                return h("div", re);
                            }
                        }
                    },{
                        title: "发票状态",
                        key: "invoiceStatus",
                        width: 180,
                        sortable: true,
                        render: (h, params) => {
                            let re = "";
                            if (params.row.isInvoice === 2) {
                                re = "未开";
                            } else if (params.row.isInvoice === 1 && params.row.invoiceStatus > 0) {
                                re = "待开";
                            } else {
                                re = "已开";
                            }
                            return h("div", re);
                        }
                    },
                    {
                        title: "支付金额",
                        key: "orderAmount",
                        width: 180,
                        sortable: true
                    },
                    {
                        title: "支付状态",
                        key: "payStatusText",
                        width: 180,
                        sortable: true
                    },
                    {
                        title: "支付日期",
                        key: "payDate",
                        width: 180,
                        sortable: true
                    },
                    {
                        title: "退款金额",
                        key: "returnAmount",
                        width: 180,
                        sortable: true
                    },
                    {
                        title: "退款日期",
                        key: "returnDate",
                        width: 180,
                        sortable: true
                    },
                    {
                        title: "操作",
                        key: "action",
                        width: 200,
                        align: "center",
                        fixed: "right",
                        render: (h, params) => {
                            return h("div", [
                                h(
                                    "Button",
                                    {
                                        props: {
                                            type: "error",
                                            size: "small"
                                        },
                                        on: {
                                            click: () => {
                                                this.$refs.returnDrawer.showDrawer(params.row);
                                            }
                                        }
                                    },
                                    "退款"
                                )
                            ]);
                        }
                    }],
                data: [], // 表单数据
                total: 0 // 表单数据总数
            }
        },
        methods: {
            init() {
                this.getDataList();
            },
            getDataList() {
                this.loading = true;
                if(this.searchForm.isInvoice == undefined){
                    this.searchForm.isInvoice = ''
                }
                if(this.searchForm.payType == undefined){
                    this.searchForm.payType = ''
                }
                if(this.searchForm.orderEntity == undefined){
                    this.searchForm.orderEntity = ''
                }
                orderList(this.searchForm).then(res => {
                    this.loading = false;
                    if (res.status === true) {
                        this.data = res.data.rows;
                        this.total = res.data.total;
                    }
                });

            },
            getDataList2() {
                this.loading = true;
                orderList().then(res => {
                    this.loading = false;
                    if (res.status === true) {
                        this.data = res.data.rows;
                        this.total = res.data.total;
                    }
                });

            },
            handleSearch() {
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                this.getDataList();
            },
            handleReset() {
                this.$refs.searchForm.resetFields();
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                this.selectDate = null;
                this.searchForm.startDate = "";
                this.searchForm.endDate = "";
                // 重新加载数据
                this.getDataList2();
            },
            edit(v) {
                this.$refs.user.edit();
            },
            dropDown() {
                if (this.drop) {
                    this.dropDownContent = "展开";
                    this.dropDownIcon = "ios-arrow-down";
                } else {
                    this.dropDownContent = "收起";
                    this.dropDownIcon = "ios-arrow-up";
                }
                this.drop = !this.drop;
            },
            selectDateRange(v) {
                if (v) {
                    this.searchForm.startDate = v[0];
                    this.searchForm.endDate = v[1];
                }
            },
            exportData() {
                window.location.href = Util.initUrl("/base/common/download", this.searchForm);
            },
            changePage(v) {
                this.searchForm.pageNumber = v;
                this.getDataList();
                this.clearSelectAll();
            },
            changePageSize(v) {
                this.searchForm.pageSize = v;
                this.getDataList();
            },
            changeSort(e) {
                this.searchForm.sort = e.key;
                this.searchForm.order = e.order;
                if (e.order === "normal") {
                    this.searchForm.order = "";
                }
                this.getDataList();
            },
            showSelect(e) {
                this.selectList = e;
                this.selectCount = e.length;
            },
            clearSelectAll() {
                this.$refs.table.selectAll(false);
            },
            edit(v) {

            },
            remove(v) {

            },
            delAll() {

            }
        },
        mounted() {
            this.init();
        }
    }
</script>
